﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using ControleRepresentante.Logic;
using ControleRepresentante.Logic.Models;
using WebSite.ViewModels.Fields;
using WebSite.Views;

namespace WebSite.ViewModels.DetailsViewModels
{
    public class UsuarioDetailsViewModel : DetailsViewModel
    {
        private readonly IAreaOperacaoRegistry m_AreaOperacaoRegistry;
        private readonly Representante m_Representante;

        public UsuarioDetailsViewModel(IAreaOperacaoRegistry areaOperacaoRegistry, Representante representante) 
            : base("Usuário")
        {
            m_AreaOperacaoRegistry = areaOperacaoRegistry;
            m_Representante = representante;
        }

        protected override IEnumerable<IField> GetGeralFields()
        {
            User user = (User) Model;
            List<IField> result = new List<IField>
            {
                new LabelField("Username", "username", user.Username),
                new LabelField("E-mail", "email", user.Email),
                new LabelField("Ativo", "ativo", user.Ativo)
            };
            result.AddRange(base.GetGeralFields());
            return result;
        }

        public bool IsOperacaoRelevant(Operacao operacao)
        {
            return m_AreaOperacaoRegistry.IsOperacaoRelevant(operacao, m_Representante);
        }

        public bool IsAreaRelevant(Area area)
        {
            return m_AreaOperacaoRegistry.IsAreaRelevant(area, m_Representante);
        }

        public Permissao GetPermissao(Area area)
        {
            return ((User) Model).Permissoes.FirstOrDefault(x => x.Area == area);
        }

        public IEnumerable<Operacao> GetOperacoes(Area area)
        {
            return m_AreaOperacaoRegistry.GetOperacoes(area);
        }

        protected override IEnumerable<TabItem> GetTabs(HtmlHelper htmlHelper)
        {
            List<TabItem> result = base.GetTabs(htmlHelper).ToList();

            User user = (User)Model;

            result.Add(new TabItem("Permissões",
                "~/Content/permissions.png",
                htmlHelper.Partial("PermissaoDetails", this)));

            StringBuilder filtroStringBuilder = new StringBuilder();
            filtroStringBuilder.Append(GetUsuarioHolderField<Fabrica>(htmlHelper, user, "Fábrica(s)", "Fabrica", x => x.Nome));
            filtroStringBuilder.Append(GetUsuarioHolderField<Cliente>(htmlHelper, user, "Cliente(s)", "Cliente", x => x.RazaoSocial));
            filtroStringBuilder.Append(GetUsuarioHolderField<Vendedor>(htmlHelper, user, "Vendedor(es)", "Vendedor", x => x.Nome));
            filtroStringBuilder.Append(GetUsuarioHolderField<Transportadora>(htmlHelper, user, "Transportadora(s)", "Transportadora", x => x.Nome));

            result.Add(new TabItem("Filtros",
                "~/Content/filter.png",
                filtroStringBuilder.ToString()));

            return result;
        }

        private static string GetUsuarioHolderField<TModel>(HtmlHelper htmlHelper, User user, string caption, string controllerName, Func<TModel, string> getName)
            where TModel : UsuariosHolder
        {
            IEnumerable<TModel> filtro = user.Filtros.OfType<TModel>();
            if (filtro.Any())
            {
                MvcHtmlString htmlString = new MvcHtmlString(String.Join(", ", filtro.Select(x => htmlHelper.ActionLink(getName(x), "Details", controllerName, new {id = x.Id}, null))));
                return htmlHelper.FieldFor(caption, htmlString).ToHtmlString();
            }
            return htmlHelper.ShowIfNotNullOrEmpty(caption, "Sem filtro").ToHtmlString();
        }
    }
}